package com.imay.live.adapter;


import android.content.Context;
import android.support.annotation.LayoutRes;
import android.util.Log;
import android.util.SparseArray;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.adapter.holder.DynamicsViewHolder;
import com.imay.live.adapter.holder.LiveViewHolder;
import com.imay.live.adapter.holder.SuggestViewHolder;
import com.imay.live.fragment.dynamics.DynamicsParams;
import com.imay.live.fragment.dynamics.OnTouchHeartListener;
import com.imay.live.model.FeedModel;
import com.imay.live.model.RoomInfoModel;
import com.imay.live.R;

import java.util.List;

/**
 * Created by chan on 2017/3/25.
 */

public class DynamicsLinearAdapter2 extends BaseQuickAdapter<Object, BaseViewHolder> {

    private DynamicsViewHolder mHolder;
    private LiveViewHolder mLiveHolder;
    private SuggestViewHolder mSuggestViewHolder;
    private OnTouchHeartListener listener;

    private SparseArray<Integer> layouts;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     */
    public DynamicsLinearAdapter2(Context context, boolean visiableFeed) {
        super(null);
        mHolder = new DynamicsViewHolder(visiableFeed);
        mLiveHolder = new LiveViewHolder(visiableFeed);
        mSuggestViewHolder = new SuggestViewHolder();

        addItemType(DynamicsParams.VIEWTYPE_LINEAR_IMAGE, R.layout.item_dynamics_linear_img);
        addItemType(DynamicsParams.VIEWTYPE_LINEAR_LIVE, R.layout.item_dynamics_linear_live);
        addItemType(DynamicsParams.VIEWTYPE_LINEAR_VIDEO, R.layout.item_dynamics_linear_mov);
        addItemType(DynamicsParams.VIEWTYPE_LINEAR_SUGGEST, R.layout.item_dynamics_linear_suggest);
    }

    protected void addItemType(int type, @LayoutRes int layoutResId) {
        if (layouts == null) {
            layouts = new SparseArray<>();
        }
        layouts.put(type, layoutResId);
    }


    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        if (getLayoutId(viewType) == 0) {
            return null;
        }
        return createBaseViewHolder(parent, getLayoutId(viewType));
    }

    private int getLayoutId(int viewType) {
        return layouts.get(viewType);
    }

    @Override
    protected int getDefItemViewType(int position) {
        if (!mData.isEmpty()) {
            Object item = mData.get(position);
            if (item instanceof FeedModel) {
                FeedModel feedModel = (FeedModel) item;
                if (feedModel.getItemType() == DynamicsParams.VIEWTYPE_LINEAR_IMAGE) {
                    return DynamicsParams.VIEWTYPE_LINEAR_IMAGE;
                } else if (feedModel.getItemType() == DynamicsParams.VIEWTYPE_LINEAR_VIDEO) {
                    return DynamicsParams.VIEWTYPE_LINEAR_VIDEO;
                }
            } else if (item instanceof RoomInfoModel) {
                return DynamicsParams.VIEWTYPE_LINEAR_LIVE;
            } else if (item instanceof List) {
                //Log.e("ttt", "111");
                return DynamicsParams.VIEWTYPE_LINEAR_SUGGEST;
            }
        }
        return DynamicsParams.VIEWTYPE_NONE;
    }

    @Override
    protected void convert(BaseViewHolder helper, Object item) {
        if (item instanceof FeedModel) {
            FeedModel feedModel = (FeedModel) item;
            if (feedModel.getItemType() == DynamicsParams.VIEWTYPE_LINEAR_IMAGE) {
                mHolder.convertImg(helper, (FeedModel) item, listener);
            } else if (feedModel.getItemType() == DynamicsParams.VIEWTYPE_LINEAR_VIDEO) {
                mHolder.convertMov(helper, (FeedModel) item, listener);
            }
        } else if (item instanceof RoomInfoModel) {
            mLiveHolder.convert(helper, (RoomInfoModel) item, listener);
        } else if (item instanceof List) {
            List list = (List) item;
            mSuggestViewHolder.convert(helper, list, listener);
        }
    }

    public void setTouchHeartListener(OnTouchHeartListener listener) {
        this.listener = listener;
    }
}
